package org.example.struct.array2;

import java.util.*;

/**
 * @author liyishan
 * @date 2024/5/22 10:15
 * @apiNote
 */

public class Test02225FindWinners {

    public List<List<Integer>> findWinners(int[][] matches) {
        Map<Integer, Integer> map = new HashMap<>();
        Set<Integer> set = new HashSet<>();
        for (int[] match : matches) {
            map.put(match[1], map.getOrDefault(match[1], 0) + 1);
            set.add(match[0]);
            set.add(match[1]);
        }
        List<Integer> winner = new ArrayList<>();
        List<Integer> loser = new ArrayList<>();
        for (int i : set) {
            if (!map.containsKey(i)) {
                winner.add(i);
            } else if (map.get(i) == 1) {
                loser.add(i);
            }
        }
        Collections.sort(winner);
        Collections.sort(loser);
        List<List<Integer>> res = new ArrayList<>();
        res.add(winner);
        res.add(loser);
        return res;
    }

    public List<List<Integer>> findWinners1(int[][] matches) {
        Map<Integer, Integer> freq = new HashMap<Integer, Integer>();
        for (int[] match : matches) {
            int winner = match[0], loser = match[1];
            freq.putIfAbsent(winner, 0);
            freq.put(loser, freq.getOrDefault(loser, 0) + 1);
        }

        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        for (int i = 0; i < 2; ++i) {
            ans.add(new ArrayList<Integer>());
        }
        for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
            int key = entry.getKey(), value = entry.getValue();
            if (value < 2) {
                ans.get(value).add(key);
            }
        }

        Collections.sort(ans.get(0));
        Collections.sort(ans.get(1));
        return ans;
    }

}
